<template>
  <div class="store-home">
    <search-bar></search-bar>
    <scroll :top="scrollTop" @onScroll="onScroll" ref="scroll">
      <!-- 图片模块 -->
      <div class="banner-warpper">
        <div
          class="banner-img"
          :style="{ backgroundImage: `url(${banner})` }"
        ></div>
      </div>
      <!-- 猜你喜欢模块 -->
      <guess-you-like :data="guessYouLike" ref="guessYouLike"></guess-you-like>
      <!-- 推荐模块 -->
      <recommend class="recommend" :data="recommend" ref="recommend" :titleText="$t('home.featured')" :btnText="$t('home.seeAll')"></recommend>
      <!-- 精选 -->
      <featured class="featured" :data="featured"></featured>
      <div class="category-list-wrapper" v-for="(item, index) in categoryList" :key="index">
        <category-book :data="item"></category-book>
      </div>
      <category class="category" :data="categories"></category>
    </scroll>
    <flap-card v-show="flapCardVisible" :data="data"></flap-card>
  </div>
</template>

<script>
import { home } from '../../api/store'
import searchBar from '../../components/home/searchBar'
import scroll from '../../components/common/Scroll'
import FlapCard from '../../components/home/FlapCard'
import GuessYouLike from '../../components/home/GuessYouLike'
import Recommend from '../../components/home/recommend'
import Featured from '../../components/home/Featured'
import CategoryBook from '@/components/home/categoryBook'
import Category from '@/components/home/category'
import { storeHomeMixin } from '../../utils/mixin'
export default {
  mixins: [storeHomeMixin],
  data() {
    return {
      scrollTop: 94,
      data: null,
      banner: '', // 存储图片url
      guessYouLike: null, // 猜你喜欢
      recommend: null, // 热卖推荐
      featured: null, // 精选
      categoryList: null, // xx 学科
      categories: null // 分类
    }
  },
  components: {
    searchBar,
    scroll,
    FlapCard,
    GuessYouLike,
    Recommend,
    Featured,
    CategoryBook,
    Category
  },
  methods: {
    // 滑动触发
    onScroll(offsetY) {
      this.setOffsetY(offsetY)
      if (offsetY > 0) {
        this.scrollTop = 54
      } else {
        this.scrollTop = 94
      }
      this.$refs.scroll.refresh()
    },
    // 随机生成图书
    RandomBooks(response) {
      const data = response.data
      console.log(data)
      const randomIndex = Math.floor(Math.random() * data.random.length)
      // 随机生成图书
      this.data = data.random[randomIndex]
      this.banner = data.banner
    },
    parseHomeData(data) {
      console.log(data)
      this.guessYouLike = data.guessYouLike
      this.recommend = data.recommend
      this.featured = data.featured
      this.categoryList = data.categoryList
      this.categories = data.categories
      console.log(this.categories)
    }
  },
  mounted() {
    home().then((response) => {
      console.log(response)
      if (response && response.status === 200) {
        this.RandomBooks(response)
        this.parseHomeData(response.data)
      }
    })
  }
}
</script>
<style lang="scss" scoped>
@import '../../assets/style/global.scss';
.store-home {
  width: 100%;
  height: 100%;
  .banner-warpper {
    width: 100%;
    padding: px2rem(10);
    box-sizing: border-box;
    .banner-img {
      width: 100%;
      height: px2rem(140);
      background-repeat: no-repeat;
      background-size: 100% 100%;
    }
  }
  .recommend {
    margin-top: px2rem(20);
  }
  .featured {
    margin-top: px2rem(20);
  }
  .category-list-wrapper {
    margin-top: px2rem(20);
  }
  .category {
    margin-top: px2rem(20);
  }
}
</style>
